package com.jyxd.web.service.patient;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jyxd.web.dao.patient.PatientExchangeRecordDao;
import com.jyxd.web.data.dto.HttpResult;
import com.jyxd.web.data.patient.PatientExchangeRecord;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.jyxd.web.config.ConstantConfig.*;
import static com.jyxd.web.util.ExecuteUtil.errorIfEmpty;
import static com.jyxd.web.util.MybatisPlusUtil.*;
import static com.jyxd.web.util.UUIDUtil.getUUID;

@Service
public class PatientExchangeRecordService {

    @Autowired
    private PatientExchangeRecordDao patientExchangeRecordDao;

    /**
     * 新增一条患者流转记录
     *
     * @param code
     * @param content
     * @param patientId
     */
    public void addPatientExchangeRecord(String code, String content, String patientId) {
        // 判断可以为重复的判断
        if (isSave(code, content, patientId))
            return;
        PatientExchangeRecord patientExchangeRecord = new PatientExchangeRecord();
        patientExchangeRecord.setContent(content);
        patientExchangeRecord.setCode(code);
        patientExchangeRecord.setPatientId(patientId);
        insertOrUpdate(patientExchangeRecord);
    }

    /**
     * 判断是否需要新增
     *
     * @param code
     * @param content
     * @param patientId
     * @return
     */
    private boolean isSave(String code, String content, String patientId) {
        if (StringUtils.equals(code, PATIENT_EXCHANGE_CODE_TRANSFER_OFFICE) ||
                StringUtils.equals(code, PATIENT_EXCHANGE_CODE_TRANSFER_BED) ||
                StringUtils.equals(code, PATIENT_EXCHANGE_OPERATION)) {
            return false;
        }
        // 判断是否为重复数据
        PatientExchangeRecord record = selectByCodeAndPatientId(code, patientId);
        if (Objects.nonNull(record)) {
            record.setContent(content);
            insertOrUpdate(record);
            return true;
        }
        return false;
    }

    /**
     * 新增或修改数据
     *
     * @param patientExchangeRecord
     */
    private void insertOrUpdate(PatientExchangeRecord patientExchangeRecord) {
        if (Objects.isNull(patientExchangeRecord))
            return;
        if (StringUtils.isEmpty(patientExchangeRecord.getId())) {
            patientExchangeRecord.setId(getUUID());
            patientExchangeRecord.setCreateTime(new Date());
            patientExchangeRecord.setUpdateTime(new Date());
            patientExchangeRecord.setStatus("1");
            patientExchangeRecordDao.insert(patientExchangeRecord);
            return;
        }
        patientExchangeRecord.setUpdateTime(new Date());
        patientExchangeRecordDao.update(patientExchangeRecord, getParamOne("id", patientExchangeRecord.getId()));
    }

    /**
     * 查询一条患者唯一一条流转记录
     *
     * @param code
     * @param patientId
     * @return
     */
    private PatientExchangeRecord selectByCodeAndPatientId(String code, String patientId) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);
        map.put("patient_id", patientId);
        map.put("status", "1");
        return patientExchangeRecordDao.selectOne(getParamList(map));
    }

    /**
     * 前端查询患者流转数据
     *
     * @param map
     * @return
     */
    public HttpResult queryDataList(Map<String, Object> map) {
        errorIfEmpty(map, "参数不存在");
        errorIfEmpty(map.get("patientId"), "患者id不存在");
        if (map.containsKey("start") && map.containsKey("size")) {
            Long start = (Long) map.get("start");
            Long size = (Long) map.get("size");
            IPage<PatientExchangeRecord> page = patientExchangeRecordDao.selectPage(new Page<>(start, size), getParamOneOrderByAsc("patient_id", map.get("patientId"), "1", "create_time"));
            return new HttpResult(200, page, "分页数据查询成功");
        }
        List<PatientExchangeRecord> list = patientExchangeRecordDao.selectList(getParamOneOrderByAsc("patient_id", map.get("patientId"), "1", "create_time"));
        return new HttpResult(200, list, "数据查询成功");
    }
}
